package com.xxr.mom.ci.charging.interworking.v2016.accepter.query;

import com.xxr.mom.ci.charging.device.entity.Gun;
import com.xxr.mom.ci.charging.interworking.base.InterfaceAccepter;
import com.xxr.mom.ci.charging.interworking.dto.InterworkingConfigDto;
import com.xxr.mom.ci.charging.interworking.v2016.AbstractV2016Service;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryEquipBusinessPolicyRequest;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryEquipBusinessPolicyResponse;
import com.xxr.mom.ci.charging.mapper.device.GunMapper;
import com.xxr.mom.ci.common.enums.InterworkingRetEnum;
import com.xxr.mom.ci.common.exception.InterworkingException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 查询业务策略信息结果
 * 概述：此接口用于设备归属运营商（客户归属运营商）向客户归属运营商（设备归属运营商）查询充电桩业务策略信息。
 * 接口名称：query_equip_business_policy
 * 接口使用方法：由客户归属运营商（设备归属运营商）平台实现此接口，设备归属运营商（客户归属运营商)平台方调用。
 *
 * @description: QueryEquipBusinessPolicyService
 * @date: 2024/8/13 22:23
 * @author: natsuki_kining
 * @version: 1.0
 */
@Service
public class QueryEquipBusinessPolicyAccepterService extends AbstractV2016Service<QueryEquipBusinessPolicyRequest, QueryEquipBusinessPolicyResponse> implements InterfaceAccepter<QueryEquipBusinessPolicyRequest, QueryEquipBusinessPolicyResponse> {

    @Autowired
    private GunMapper gunMapper;

    @Override
    public void validateRequestParam(QueryEquipBusinessPolicyRequest queryEquipBusinessPolicyRequest) {
        String equipBizSeq = queryEquipBusinessPolicyRequest.getEquipBizSeq();
        String connectorId = queryEquipBusinessPolicyRequest.getConnectorId();
        if (StringUtils.isBlank(equipBizSeq) || StringUtils.isBlank(connectorId)) {
            throw new InterworkingException(InterworkingRetEnum.BUSINESS_PARAMETER_ERROR);
        }
    }

    @Override
    public QueryEquipBusinessPolicyResponse dataHandler(QueryEquipBusinessPolicyRequest queryEquipBusinessPolicyRequest, InterworkingConfigDto configDto) {
        QueryEquipBusinessPolicyResponse response = new QueryEquipBusinessPolicyResponse();
        response.setEquipBizSeq(queryEquipBusinessPolicyRequest.getEquipBizSeq());
        response.setConnectorId(queryEquipBusinessPolicyRequest.getConnectorId());

        Gun gun = gunMapper.selectByConnectorId(queryEquipBusinessPolicyRequest.getConnectorId());
        if (Objects.isNull(gun)) {
            response.setSuccStat(1);
            response.setFailReason(1);
            return response;
        }


        response.setSuccStat(0);
        response.setFailReason(0);

        // TODO 查询计费
        response.setSumPeriod(null);
        response.setPolicyInfos(null);

        return response;
    }

}
